package com.example.smartcity.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartcity.R;
import com.example.smartcity.bean.NewsListBean;
import com.example.smartcity.net.MyHttpCallBack;
import com.example.smartcity.net.OkhttpHelper;

import java.util.HashMap;

/**
 * 分类新闻
 *
 * @author YSJ
 * @date 2021/4/22 : 21:27
 */
public class NewClassifyFragment extends Fragment {

    private String type;

    private View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public NewClassifyFragment(String type) {
        this.type = type;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.fragment_news_classify, container, false);
        view = inflate;
        try {
            getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inflate;
    }

    private void getData() throws Exception {
        HashMap<String, String> map = new HashMap<>();
        map.put("pageNum", "1");
        map.put("pageSize", "10");
        map.put("pressCategory", type);

        new OkhttpHelper().<NewsListBean>get(
                map,
                "http://124.93.196.45:10002/press/press/list",
                getActivity(),
                new MyHttpCallBack<NewsListBean>() {
                    @Override
                    public void onResponse(NewsListBean data, String json) {
                        initView(data);
                    }
                },
                NewsListBean.class
        );
    }

    private void initView(NewsListBean data) {
        RecyclerView mRecyclerView = view.findViewById(R.id.mRecyclerView);
        mRecyclerView.setAdapter(new RecyclerView.Adapter() {
            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_layout_new, null, false);
                return new MyViewHolder(inflate);
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                MyViewHolder holder1 = (MyViewHolder) holder;
                Glide.with(getActivity())
                        .load("http://124.93.196.45:10002"+data.getRows().get(position).getImgUrl())
                        .into(holder1.imageView);

                holder1.content.setText(data.getRows().get(position).getContent());
                holder1.time.setText(data.getRows().get(position).getCreateTime());
                holder1.title.setText(data.getRows().get(position).getTitle());

            }

            @Override
            public int getItemCount() {
                return data.getRows().size();
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }


    class MyViewHolder extends RecyclerView.ViewHolder {


        public ImageView imageView;
        public TextView title;
        public TextView content;
        public TextView time;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
            time = itemView.findViewById(R.id.time);

        }
    }
}
